package com.example.spring_mysql.configuration

import com.example.spring_mysql.interceptor.LoggingInterceptor
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.annotation.Bean
import org.springframework.http.HttpMethod
import org.springframework.integration.annotation.InboundChannelAdapter
import org.springframework.integration.annotation.ServiceActivator
import org.springframework.integration.channel.DirectChannel
import org.springframework.integration.core.MessageSource
import org.springframework.integration.dsl.IntegrationFlow
import org.springframework.integration.dsl.Pollers
import org.springframework.integration.file.FileReadingMessageSource
import org.springframework.integration.file.filters.SimplePatternFileListFilter
import org.springframework.integration.http.dsl.Http
import org.springframework.messaging.Message
import org.springframework.messaging.MessageChannel
import org.springframework.messaging.MessageHandler
import org.springframework.stereotype.Component
import java.io.File


@Suppress("INACCESSIBLE_TYPE")
@Component
class BeanFac {
    @Bean
    fun getNewLoggingInterceptor(): LoggingInterceptor {
        return LoggingInterceptor()
    }

    @Bean
    fun myInputChannel(): MessageChannel {
        return DirectChannel()
    }

    @Bean
    fun myMessageHandler(): MessageHandler {
        return MessageHandler { message ->
            System.out.println("Received: ${message.payload}")
        }
    }

    @Bean
    fun flow(@Qualifier("myMessageHandler") messageHandler: MessageHandler): IntegrationFlow {
        return IntegrationFlow.from("myInputChannel").handle(messageHandler) // ✅ 不要手动调用 messageHandler()
            .get()
    }

//    @Bean
//    @InboundChannelAdapter(value = "fileInputChannel", poller = Poller(fixedDelay = "1000"))
//    fun fileReadingMessageSource(): MessageSource<File> {
//        val source = FileReadingMessageSource()
//        val file=File("input")
//        source.setDirectory(file)
//        println("absolutePath:"+file.absolutePath)
//        source.setFilter(SimplePatternFileListFilter("*.txt"))
//        return source
//    }

//    @Bean
//    @InboundChannelAdapter(value = "fileInputChannel")
//    fun fileReadingMessageSource(): MessageSource<File> {
//        val source = FileReadingMessageSource()
//        source.setDirectory(File("input").absoluteFile) // 改这里为绝对路径
//        source.setFilter(SimplePatternFileListFilter("*.txt"))
//        // 启用 WatchService 监听
//        source.isUseWatchService = true
//        source.setWatchEvents(FileReadingMessageSource.WatchEventType.CREATE)
//        return source
//    }
//
//    @Bean
//    @ServiceActivator(inputChannel = "fileInputChannel")
//    fun fileHandler(): MessageHandler {
//        return MessageHandler { message: Message<*> ->
//            val file: File = message.payload as File
//            println("Processing file: " + file.getName())
//        }
//    }

    @Bean
    fun fileListeningFlow(): IntegrationFlow {
        val source = FileReadingMessageSource().apply {
            setDirectory(File("input").absoluteFile)
            setFilter(SimplePatternFileListFilter("*.txt"))
            isUseWatchService = true
            setWatchEvents(FileReadingMessageSource.WatchEventType.CREATE)
        }
        return IntegrationFlow.from(source, { e ->
//            e.poller(Pollers.fixedDelay(1000)) // 1秒轮询
        }).handle { message ->
            val file = message.payload as File
            println("Detected new file: ${file.name}")
        }.get()
    }

    @Bean
    fun httpInboundFlow(): IntegrationFlow {
        return IntegrationFlow
            .from(
                Http.inboundGateway("/receive")
                    .requestPayloadType(String::class.java)
            )
            .handle { message ->
                val payload = message.payload as String
                println("Received via HTTP: $payload")
                "Received: $payload"
            }
            .get()
    }

    @Bean
    fun httpProxyFlow(): IntegrationFlow {
        return IntegrationFlow
            .from(
                Http.inboundGateway("/proxy")
                    .requestPayloadType(String::class.java)
            )
            .handle(
                Http.outboundGateway("http://127.0.0.1:8080/hello")
                    .httpMethod(HttpMethod.GET)
                    .expectedResponseType(String::class.java)
            )
            .get()
    }

}